//
//  EditPersonViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/12/5.
//  Copyright © 2016年 王少泽. All rights reserved.
//

#import "EditPersonViewController.h"
#import "PersonViewController.h"


@interface EditPersonViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation EditPersonViewController
{
    UITableView* _editTable;
}
- (void)viewWillDisappear:(BOOL)animated

{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    //写表格内容
    
    [self lazyTableview];
    [self.view addSubview:_editTable];
    
    
    
}
#pragma mark tableview的懒加载方法
- (UITableView* )lazyTableview
{
    if (_editTable == nil) {
        _editTable = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height + 20) style:UITableViewStyleGrouped];
        _editTable.dataSource = self;
        _editTable.delegate = self;
    }
    return _editTable;
}
#pragma mark tableview的数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* ruse = @"ruse";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ruse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ruse];
    }
    NSArray* dataSource = @[@"个性签名",@"性别",@"生日",@"星座",@"血型"];
    cell.textLabel.text = dataSource[indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma mark tableview的页眉高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 190;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 800;
}

- (UIView* )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    //    headView.layer.masksToBounds = YES;
    //按钮与背景图
    UIImageView* bakcGroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 190)];
    bakcGroundImage.image = [UIImage imageNamed:@"personBack.jpg"];
    //按钮
    UIButton* personInfo = [UIButton buttonWithType:UIButtonTypeCustom];
    [personInfo setBackgroundImage:[UIImage imageNamed:@"testtouxiang.jpg"] forState:UIControlStateNormal];
    personInfo.layer.masksToBounds = YES;
    personInfo.layer.cornerRadius = 32;
    personInfo.frame = CGRectMake(self.view.frame.size.width / 2 - 32 , 85- 44, 64, 64);
    //编辑更换头像的方法
    [personInfo addTarget:self action:@selector(personEdit:) forControlEvents:UIControlEventTouchUpInside];
    
    [bakcGroundImage addSubview:personInfo];
    bakcGroundImage.userInteractionEnabled = YES;
    //写一个返回按钮
    UIButton* backView = [UIButton buttonWithType:UIButtonTypeCustom];
    [backView setBackgroundImage:[UIImage imageNamed:@"backBtn"] forState:UIControlStateNormal];
    backView.frame = CGRectMake(20, 20, 24, 24);
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 12;
    //添加返回按钮的方法
    [backView addTarget:self action:@selector(backToView:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:bakcGroundImage];
    [headView addSubview:backView];
    
    return headView;
}
- (void)personEdit:(UIButton* )sender
{
    UIAlertController* editHead = [[UIAlertController alloc]init];
    //editHead.preferredStyle = UIAlertControllerStyleActionSheet;
    UIAlertAction* album = [UIAlertAction actionWithTitle:@"从手机相册来选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            
            picker.delegate = self;
            
            picker.allowsEditing = YES;
            
            //打开相册选择照片
            
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            [self presentViewController:picker animated:YES completion:nil];
            
        }
        
    }];
    UIAlertAction* camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    UIAlertAction* canncel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [editHead addAction:camera];
    [editHead addAction:album];
    [editHead addAction:canncel];
    
    
    [self presentViewController:editHead animated:YES completion:nil];
    
}
- (void)backToView:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView* )tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 200)];
    view.backgroundColor = [UIColor whiteColor];
    
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
